//
//  ViewController.swift
//  ZLNavigationRoot
//
//  Created by Zz_l on 01/08/2023.
//  Copyright (c) 2023 Zz_l. All rights reserved.
//

import UIKit
import ZLNavigationRoot
import ZLExtension

class ViewController: ZLBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.contentView.backgroundColor = ZLRandomColor()
        let nav = self.getTestButton(title: "导航隐藏显示", action: #selector(navAction))
        nav.frame = CGRect(x: 50, y: 100, width: 200, height: 50)
        self.view.addSubview(nav)
        
        let tabbar = self.getTestButton(title: "tabbar隐藏显示", action: #selector(tabbarAction))
        tabbar.frame = CGRect(x: 50, y: 200, width: 200, height: 50)
        self.view.addSubview(tabbar)
        
        let push = self.getTestButton(title: "push", action: #selector(pushAction))
        push.frame = CGRect(x: 50, y: 300, width: 200, height: 50)
        self.view.addSubview(push)
        
        /// 设置背景图
        guard let tabbar = self.tabBarController as? ZLTabBarRoot else { return }
        tabbar.backgroundImageView.image = UIImage(named: "tabbar")
    }
    /// 导航隐藏显示
    @objc func navAction() {
        guard let navc = self.navigationController else { return }
//        navc.setNavigationBarHidden(!navc.isNavigationBarHidden, animated: true)
        navc.isNavigationBarHidden = !navc.isNavigationBarHidden
    }
    /// tabbar隐藏与现实
    @objc func tabbarAction() {
        guard let tabbar = self.tabBarController as? ZLTabBarRoot else { return }
        if tabbar.tabBar.isHidden {
            tabbar.showTabBar(animated: true)
        } else {
            tabbar.hiddenTabBar(animated: true)
        }
    }
    /// 跳转
    @objc func pushAction() {
        let count = self.navigationController?.viewControllers.count ?? 0
        let vc = ViewController(title: String(count))
        if count == 2 {
            /// 禁止左滑返回
            vc.gestureBack = false
        }
        if count == 3 {
            /// 隐藏tabbar（基础用法）
            vc.hidesBottomBarWhenPushed = true
        }
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    func getTestButton(title: String, action: Selector) -> UIButton {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitle(title, for: .normal)
        button.setTitleColor(UIColor.blue, for: .normal)
        button.addTarget(self, action: action, for: .touchUpInside)
        return button
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

